package com.taotao.common.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 *	Jedis操作redis常用方法工具类 
 */
@Service
public class RedisService {

	//注入分片的连接池
	//RedisService中需要注入ShardedJedisPool，而并不是每一个依赖common的工程都需要Redis功能，那么这些工程就不会配置ShardedJedisPool
	//注入就会失败，因此这里必须设置required = false 。这样就可以按需加载

	@Autowired(required = false)
	private ShardedJedisPool shardedJedisPool;

	/**
	 * @Description:设置key value
	 * @param key
	 * @param value
	 * @return
	 * @author mingxu
	 */
	public String set(String key, String value) {
		ShardedJedis shardedJedis = null;
		try {
			//从池中获取jedis
			shardedJedis = shardedJedisPool.getResource();

			//设置值
			return shardedJedis.set(key, value);

		} finally {
			if (null != shardedJedis) {
				//用完之后放回连接池
				shardedJedis.close();
			}
		}
	}

	/**
	 * @Description:根据key 获取 value
	 * @param key
	 * @return
	 * @author mingxu
	 */
	public String get(String key) {
		ShardedJedis shardedJedis = null;
		try {
			//从池中获取jedis
			shardedJedis = shardedJedisPool.getResource();

			return shardedJedis.get(key);

		} finally {
			if (null != shardedJedis) {
				//关闭，检测连接是否有效，有效则返回到连接池，无效则重置状态
				shardedJedis.close();
			}
		}
	}

	/**
	 * @Description:设置生存时间
	 * @param key
	 * @param seconds
	 * @return
	 * @author mingxu
	 */
	public Long expire(String key, int seconds) {
		ShardedJedis shardedJedis = null;
		try {
			//从池中获取jedis
			shardedJedis = shardedJedisPool.getResource();

			return shardedJedis.expire(key, seconds);

		} finally {
			if (null != shardedJedis) {
				//关闭，检测连接是否有效，有效则返回到连接池，无效则重置状态
				shardedJedis.close();
			}
		}
	}

	/**
	 * @Description:清除设置的生存时间
	 * @param key
	 * @return
	 * @author mingxu
	 */
	public Long persist(String key) {
		ShardedJedis shardedJedis = null;
		try {
			//从池中获取jedis
			shardedJedis = shardedJedisPool.getResource();

			return shardedJedis.persist(key);

		} finally {
			if (null != shardedJedis) {
				//关闭，检测连接是否有效，有效则返回到连接池，无效则重置状态
				shardedJedis.close();
			}
		}
	}

	/**
	 * @Description:设置key value，并设置生存时间
	 * @param key
	 * @param seconds
	 * @param value
	 * @return
	 * @author mingxu
	 * 
	 */
	public String setex(String key, int seconds, String value) {
		ShardedJedis shardedJedis = null;
		try {
			//从池中获取jedis
			shardedJedis = shardedJedisPool.getResource();

			return shardedJedis.setex(key, seconds, value);

		} finally {
			if (null != shardedJedis) {
				//关闭，检测连接是否有效，有效则返回到连接池，无效则重置状态
				shardedJedis.close();
			}
		}
	}

	/**
	 * @Description:根据key删除value
	 * @param key
	 * @return
	 * @author mingxu
	 */
	public Long del(String key) {
		ShardedJedis shardedJedis = null;
		try {
			//从池中获取jedis
			shardedJedis = shardedJedisPool.getResource();

			return shardedJedis.del(key);

		} finally {
			if (null != shardedJedis) {
				//关闭，检测连接是否有效，有效则返回到连接池，无效则重置状态
				shardedJedis.close();
			}
		}
	}

}
